{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from __future__ import print_function, division, absolute_import\n",
    "\n",
    "import GPy\n",
    "import numpy as np\n",
    "import matplotlib as mpl\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "\n",
    "import safeopt\n",
    "\n",
    "mpl.rcParams['figure.figsize'] = (20.0, 10.0)\n",
    "mpl.rcParams['font.size'] = 20\n",
    "mpl.rcParams['lines.markersize'] = 20"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Define a kernel and function\n",
    "\n",
    "Here we define a kernel. The function is drawn at random from the GP and is corrupted my Gaussian noise"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Measurement noise\n",
    "noise_var = 0.05 ** 2\n",
    "\n",
    "# Bounds on the inputs variable\n",
    "bounds = [(-10., 10.)]\n",
    "parameter_set = safeopt.linearly_spaced_combinations(bounds, 1000)\n",
    "\n",
    "# Define Kernel\n",
    "kernel = GPy.kern.RBF(input_dim=len(bounds), variance=2., lengthscale=1.0, ARD=True)\n",
    "\n",
    "# Initial safe point\n",
    "x0 = np.zeros((1, len(bounds)))\n",
    "\n",
    "# Generate function with safe initial point at x=0\n",
    "def sample_safe_fun():\n",
    "    while True:\n",
    "        fun = safeopt.sample_gp_function(kernel, bounds, noise_var, 100)\n",
    "        if fun(0, noise=False) > 0.5:\n",
    "            break\n",
    "    return fun"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Interactive run of the algorithm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Define the objective function\n",
    "fun = sample_safe_fun()\n",
    "\n",
    "# The statistical model of our objective function\n",
    "gp = GPy.models.GPRegression(x0, fun(x0), kernel, noise_var=noise_var)\n",
    "\n",
    "# The optimization routine\n",
    "opt = safeopt.SafeOptSwarm(gp, 0., bounds=bounds, threshold=0.2)\n",
    "# opt = safeopt.SafeOpt(gp, parameter_set, 0., lipschitz=None, threshold=0.2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def plot_gp():\n",
    "    # Plot the GP\n",
    "    opt.plot(1000)\n",
    "    # Plot the true function\n",
    "    plt.plot(parameter_set, fun(parameter_set, noise=False), color='C2', alpha=0.3)\n",
    "    \n",
    "plot_gp()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Obtain next query point\n",
    "x_next = opt.optimize()\n",
    "# Get a measurement from the real system\n",
    "y_meas = fun(x_next)\n",
    "# Add this to the GP model\n",
    "opt.add_new_data_point(x_next, y_meas)\n",
    "\n",
    "plot_gp()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.3"
  },
  "widgets": {
   "state": {
    "5df21601a9be49cb816a6ae251a4f52d": {
     "views": [
      {
       "cell_index": 5
      }
     ]
    },
    "9bc9833ac1af4a64b4b160ad65ca8e0e": {
     "views": [
      {
       "cell_index": 5
      }
     ]
    }
   },
   "version": "1.2.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
